package top.i51213.fastaction.uri;

import android.os.Environment;

import androidx.annotation.NonNull;

import java.io.File;

import top.i51213.fastaction.util.IOUtil;

public abstract class MimeType {
    private final String main;
    private final String subType;
    private final String defaultExt;
    private MimeType(String main,String subType,String defaultExt){
        this.main=main;
        this.subType=subType;
        this.defaultExt=defaultExt;
    }
    public final String get(){
        return main+"/"+subType;
    }
    public final String getMainMimeType(){
        return main+"/*";
    }
    public final String getExt(){
        return defaultExt;
    }

    public File getDefaultDir(){
        File dir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS), "FastAction");
        if (!dir.exists()) {
            dir.mkdirs();
        }
        return dir;
    }

    public static MimeType fromFileName(String fileName) {
        return fromExt(IOUtil.getExt(fileName));
    }

    public static MimeType fromExt(String ext) {
        switch (ext) {
            case ".jpg":
            case ".jpeg":
                return Image.Jpeg;
            case ".png":
                return Image.Png;
            case ".webp":
                return Image.Webp;
            case ".bmp":
                return Image.Bmp;
            case ".zip":
                return Others.Any;
        }
        return Others.Any;
    }

    @NonNull
    @Override
    public final String toString() {
        return this.getClass().getSimpleName() + "{" +
                "mimetype='" + get() + '\'' +
                ", ext='" + getExt() + '\'' +
                '}';
    }

    public static final class Image extends MimeType {
        public static final MimeType Jpeg = new Image("jpeg", ".jpg");
        public static final MimeType Png = new Image("png", ".png");
        public static final MimeType Webp = new Image("webp", ".webp");
        public static final MimeType Bmp = new Image("bmp", ".bmp");

        private Image(String subType, String defaultExt) {
            super("image",subType,defaultExt);
        }

        public final File getDefaultDir() {
            File dir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "FastAction");
            if (!dir.exists()) {
                dir.mkdirs();
            }
            return dir;
        }
    }

    public static class Application extends MimeType {
        public static final MimeType Pdf = new Application("pdf", ".pdf");
        public static final MimeType Zip = new Application("zip", ".zip");
        private Application(String subType, String defaultExt) {
            super("application", subType, defaultExt);
        }
    }

    public static class Others extends MimeType {
        public static final Others Any = new Others("*","*","");

        private Others(String main, String subType, String defaultExt) {
            super(main, subType, defaultExt);
        }
    }
}
